package com.ys.controller.api.storage;

import com.alibaba.fastjson.JSON;
import com.ys.config.MenuId;
import com.ys.entity.base.TbCountry;
import com.ys.entity.finance.TcwPaymentReturn;
import com.ys.entity.finance.TcwPaymentReturnItem;
import com.ys.entity.storage.TccLogisticsServiceProvider;
import com.ys.entity.storage.TccWarehouse;
import com.ys.entity.sys.YsCol;
import com.ys.mapper.storage.StorageMapper;
import com.ys.service.storage.TccLogisticsServiceProviderService;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.UploadService;
import com.ys.util.ExportExcelUtils;
import com.ys.util.admin.ConditionUtil;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @Author: fxz
 * @CreateTime: 2024-12-05
 * @Description: 物流服务商
 * @Version: 1.5
 */
@Controller
@RequestMapping("/api/inter/tccLogisticsServiceProvider")
public class ApiInterTccLogisticsServiceProviderController {
    private static final Logger logger = LoggerFactory.getLogger(ApiInterTccLogisticsServiceProviderController.class);
    
    @Autowired
    private TccLogisticsServiceProviderService tccLogisticsServiceProviderService;

    @Autowired
    private CommonApiService<TccLogisticsServiceProvider> commonApiService;

    //菜单id
    private Long menuId = MenuId.TCC_LogisticsServiceProvider;

    @Autowired
    private StorageMapper storageMapper;
    /**
     * 查询
     */
    @RequestMapping("/search")
    @ResponseBody
    public LayuiTableVoJA search(Model model, HttpServletRequest request, HttpServletResponse response,
                                 @ModelAttribute TccLogisticsServiceProvider cond, String conditions
    ){
        try {
            cond = setSeachSql(cond);
            LayuiTableVoJA setList = commonApiService.searchLay(cond);
            return setList;
        }catch (Exception e){
            logger.error("查询出错：",e.getMessage(),e);
            LayuiTableVoJA tableVoJA = new LayuiTableVoJA();
            tableVoJA.setCode(1);
            tableVoJA.setMsg("查询出错，请稍后重试！");
            return tableVoJA;
        }
    }



    /**
    /**
     * 设置查询的cond的sql设置
     * @param cond
     * @return
     */
    public TccLogisticsServiceProvider setSeachSql(TccLogisticsServiceProvider cond){

        cond.addLeftJoin("TCC_LogisticsServiceProviderItem B ON B.Pid=A.id")
                .addField("B.ContactPerson contactPerson,B.ContactNumber contactNumber,B.BankAccountName bankAccountName,B.OpeningBank openingBank,B.AccountNumber accountNumber,B.Contract contract")
                .setOrderby("A.Abbreviation ASC");

        cond = commonApiService.dataAuth(menuId, cond);
        return  cond;
    }





    /*
     * 新增
     */
    @RequestMapping("/toAdd")
    @ResponseBody
    public R add(HttpServletRequest request,@RequestParam Map<String, Object> params, Long id) {
        try {

            //修改
            return tccLogisticsServiceProviderService.add( params, id);
        } catch (Exception e) {
            logger.error("物流服务商新增出错：", e.getMessage(), e);
            return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
        }
    }

    /**
     * 修改
     */
    @RequestMapping("/toEdit")
    @ResponseBody
    public R edit(HttpServletRequest request,@RequestParam Map<String, Object> params, Long menuId,Long id) {
        try {
            return tccLogisticsServiceProviderService.save(params,menuId, id);
        } catch (Exception e) {
            logger.error("物流服务商编辑出错：", e.getMessage(), e);
            return R.error(e.getMessage());
        }
    }

    /**
     * 导出
     */
    @RequestMapping("/export")
    @ResponseBody
    public R export(@ModelAttribute TccLogisticsServiceProvider cond, String conditions){
        try {
            cond.setPage(0).setSize(999999);
            cond = setSeachSql(cond);

            TccLogisticsServiceProvider tccLogisticsServiceProvider = JSON.parseObject(JSON.toJSONString(cond), TccLogisticsServiceProvider.class);
            TccWarehouse tccWarehouse = JSON.parseObject(JSON.toJSONString(cond), TccWarehouse.class);

            //获取菜单字段数据
            List<YsCol> listField = new YsCol()
                    .setMenuId(menuId)
                    .setFrontShow(1)
                    .setFrontExport(1)
                    .setOrderby("A.SortNo asc")
                    .queryList();
            //获取数据
            List<Map<String, Object>> listData = cond.queryMap();

            //处理数据
            ExportExcelUtils<TccLogisticsServiceProvider> util = new ExportExcelUtils<>(new TccLogisticsServiceProvider());
            String toFilePath = util.exportExcel(listData, listField, "物流服务维护单");
            UploadService.UploadDto dto = commonApiService.uploadLocalToServer(toFilePath);
            return R.ok(dto.getUrl());
        }catch (Exception e){
            logger.error("导出出错：",e.getMessage(),e);
            return R.error("导出失败，请稍后重试！");
        }
    }



    /**
     * 查询
     */
    @RequestMapping("/searchByModal")
    @ResponseBody
    public LayuiTableVoJA searchByModal(Model model, HttpServletRequest request, HttpServletResponse response,
                                 @ModelAttribute TccLogisticsServiceProvider cond, String conditions
    ){
        try {
            cond = setSeachSql(cond);
            cond.setStatus(1);
            LayuiTableVoJA setList = commonApiService.searchLay(cond);
            return setList;
        }catch (Exception e){
            logger.error("查询出错：",e.getMessage(),e);
            LayuiTableVoJA tableVoJA = new LayuiTableVoJA();
            tableVoJA.setCode(1);
            tableVoJA.setMsg("查询出错，请稍后重试！");
            return tableVoJA;
        }
    }


    /**
     * 预付款申请详情调用
     * @return
     */
    @RequestMapping("/searchWl")
    @ResponseBody
    public R searchGsXq(@ModelAttribute TccLogisticsServiceProvider cond, String conditions){
        List<Map<String, Object>> sonlist = new TccLogisticsServiceProvider()
                .addLeftJoin("TCC_LogisticsServiceProviderItem B ON B.Pid = A.Id")
                .addField("B.ContactPerson contactPerson")
                .addField("B.ContactNumber contactNumber")
                .addField("B.BankAccountName bankAccountName")
                .addField("B.OpeningBank openingBank")
                .addField("B.AccountNumber accountNumber")
                .addField("B.Contract contract")
                .setStatus(1)
                .queryMap();
        return  R.ok("成功",sonlist);
    }

    /**
     * 删除列表
     */

    @RequestMapping("/toDelByItem")
    @ResponseBody
    public R toDelByItem(HttpServletRequest request,@RequestParam("list") List<String> idList){
        try {
            //删除
            for (int i = 0; i < idList.size(); i++) {
                tccLogisticsServiceProviderService.toDelByItem(Long.parseLong(idList.get(i)));
            }
            return R.ok();
        } catch (Exception e) {
            logger.error("删除出错：",e.getMessage(),e);
            return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
        }
    }
}
